 //email:batchmichaelbxkxyj@gmail.com
//website  vpn open:  http://web3incubators.com/
//Telegram ： https://t.me/gamecode999
//电子邮件batchmichaelbxkxyj@gmail.com
//国外网站 vpn打开 http://web3incubators.com/
//电报https://t.me/gamecode999

import { ENUM_GAME_MODE, ENUM_GAME_STATUS } from '../Enum';
import Player from '../game/Player';
import { Point } from '../plugins/AINavigate';

const STORAGE_KEY = 'GAME_STORAGE_KEY'

export type ICallBack = {
    status: boolean,
    data: any,
    msg: string
}

export type IUser = {
    openid: string,
    nickName: string,
    gender: number,
    avatar: string
}
/**
 * BOARD 尺寸
 * COLORS 颜色
 * COMBINE 消除数
 * LEVELS 几种颜色, 人总数
 */
export const GAME_DATA = {
    BOARD_SIZE: 82,
    COLORS: ['#CCCCCC', '#FFE313', '#2D7AFF', '#6DF212', '#FF70FA', '#FF1E62', '#4D4D4D', '#7500D6', '#F19E40', '#63CFF8'], // 灰 黄 蓝 绿 粉 红 黑 紫 橙 浅蓝
    COMBINE: 3,
    LEVELS: [{ type: 4, total: 12 }, { type: 5, total: 18 }, { type: 4, total: 24 }, { type: 7, total: 30 }, { type: 7, total: 36 }, { type: 7, total: 42 }, { type: 6, total: 60 }, { type: 6, total: 96 }],
    CAR_POSX: [-1080, -600, -480, 0, 600]
}

export default class DataManager {

    private static _instance: any = null

    static getInstance<T>(): T {
        if (this._instance === null) {
            this._instance = new this()
        }

        return this._instance
    }

    static get instance() {
        return this.getInstance<DataManager>()
    }

    // 游戏模式
    mode: ENUM_GAME_MODE = ENUM_GAME_MODE.NORMAL
    // 游戏状态
    status: ENUM_GAME_STATUS = ENUM_GAME_STATUS.UNRUNING
    // 加载进度
    loadingRate: number = 0
    // 声音开启
    _isMusicOn: boolean = true
    _isSoundOn: boolean = true
    // 更多游戏
    games: any[] = [
        { title: '进厂打螺丝', icon: 'logo_screw', appid: '', url: 'https://store.cocos.com/app/search?name=carolsail' },
        { title: '趣味聊天', icon: 'logo_chat', appid: '', url: 'https://store.cocos.com/app/search?name=carolsail' },
        { title: '挪车大挑战', icon: 'logo_car', appid: '', url: 'https://store.cocos.com/app/search?name=carolsail' },
        { title: '小鸟消消乐', icon: 'logo_bird', appid: '', url: 'https://store.cocos.com/app/search?name=carolsail' },
        { title: '胶囊2048', icon: 'logo_2048', appid: '', url: 'https://store.cocos.com/app/search?name=carolsail' },
    ]
    // 关卡
    level: number = 1
    levelMax: number = 1

    // 体力系统
    power: number = 10 // 初始体力
    powerMax: number = 10 // 体力最大值
    powerCollectByVideo: number = 1 // 广告回复
    powerRefreshTime: number = 120 // 间隔刷新
    lastPowerRefreshTime: number = 0 // 每次刷新纪录点
    lastPowerUpdateTime: number = 0 //  上一次更新时间

    // 胜负场数
    countWin: number = 0
    countLose: number = 0
    // 胜负得分
    scoreWin: number = 20
    scoreLose: number = 5

    // 地图
    mapPoints: Map<number, Point> = new Map()
    mapBoards: Map<number, cc.Node> = new Map()

    // 站台站位
    mapTargetMark: Map<number, Player> = new Map()
    targetMark: number[] = []
    targetMarkLimit: number = 4

    // 人数
    levelPlayerTotal: number = 0
    levelPlayerClear: number = 0

    // 技能换位
    skillPlayerComp: Player[] = []
    skillBoardNode: cc.Node = null
    skillBoardColor: string = ''

    // 补位状态
    isCovering: boolean = false

    // 当前ai
    currentAI: IUser = null
    levelAIClear: number = 0

    get isMusicOn() {
        return this._isMusicOn
    }

    set isMusicOn(data: boolean) {
        this._isMusicOn = data
    }

    get isSoundOn() {
        return this._isSoundOn
    }

    set isSoundOn(data: boolean) {
        this._isSoundOn = data
    }

    reset() {
        this.status = ENUM_GAME_STATUS.UNRUNING
        this.mapPoints = new Map()
        this.mapBoards = new Map()
        this.mapTargetMark = new Map()
        this.targetMark = []
        this.targetMarkLimit = 4
        this.levelPlayerTotal = 0
        this.levelPlayerClear = 0
        this.skillPlayerComp = []
        this.skillBoardNode = null
        this.skillBoardColor = ''
        this.isCovering = false
    }

    save() {
        cc.sys.localStorage.setItem(STORAGE_KEY, JSON.stringify({
            isSoundOn: this.isSoundOn,
            isMusicOn: this.isMusicOn,
            level: this.level,
            levelMax: this.levelMax,
            power: this.power,
            lastPowerRefreshTime: this.lastPowerRefreshTime,
            lastPowerUpdateTime: this.lastPowerUpdateTime,
            countWin: this.countWin,
            countLose: this.countLose,
        }))
    }

    restore() {
        const _data = cc.sys.localStorage.getItem(STORAGE_KEY) as any
        try {
            const data = JSON.parse(_data)
            this.isMusicOn = data?.isMusicOn === false ? false : true
            this.isSoundOn = data?.isSoundOn === false ? false : true
            this.level = typeof data.level == 'number' ? data.level : 1
            this.levelMax = typeof data.levelMax == 'number' ? data.levelMax : 1
            this.power = typeof data.power == 'number' ? data.power : 10
            this.lastPowerRefreshTime = typeof data.lastPowerRefreshTime == 'number' ? data.lastPowerRefreshTime : 0
            this.lastPowerUpdateTime = typeof data.lastPowerUpdateTime == 'number' ? data.lastPowerUpdateTime : 0
            this.countWin = typeof data.countWin == 'number' ? data.countWin : 0
            this.countLose = typeof data.countLose == 'number' ? data.countLose : 0
        } catch {
            this.isMusicOn = true
            this.isSoundOn = true
            this.level = 1
            this.levelMax = 1
            this.power = 10
            this.lastPowerRefreshTime = 0
            this.lastPowerUpdateTime = 0
            this.countWin = 0
            this.countLose = 0
        }
    }
}
